package net.frenopatico.kynes.searchr.controllers;

import net.frenopatico.kynes.annotation.LazyController;
import net.frenopatico.kynes.searchr.models.Message;
import net.frenopatico.kynes.searchr.services.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Controller encapsulating Message Bussines Logic.
 * 
 * @author Eduardo Ramírez <edu.kynes@gmail.com>
 * 
 */
@LazyController
public class MessageController {

    /**
     * Message Services.
     */
    @Autowired
    private MessageService messageService; // NOPMD

    /**
     * 
     * Saves the given message.
     *
     * @param messageText Name parameter.
     * @param message model attribute.
     * @return The path to the JSP.
     */
    @RequestMapping("/save/{messageText}")
    public final String saveMessage( @PathVariable final String messageText, @ModelAttribute final Message message ) {

    	message.setText( messageText );
    	messageService.save( message );

    	return "hello/hello";
    }

    /**
     * @param messageId
     *            Hello id.
     * @param model
     *            Model.
     * @return The JSP.
     */
    @RequestMapping("/show/{messageId}")
    public final String showMessage( @PathVariable final String messageId, final Model model ) {
        Message savedMessage = messageService.get( Long.valueOf( messageId ) );
        
        model.addAttribute( "message", savedMessage );

        return "hello/hello";
    }
}
